﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
using GrainManage.Data.Util;
using GrainManage.Data.Model.Filter;
using GrainManage.Data.Model.Table;
using GrainManage.Data.Model.View;
using GrainManage.Data.Model;

namespace GrainManage.Data.Service
{
    public class ContactService : BaseService
    {

        public ContactService(GrainManage.Data.Access.AccessBase proc, GrainManage.Data.Pattern.IPattern pattern) : base(proc, pattern) { }

        public List<string> GetContactName(IPageable page, string creator)
        {
            var service = new CommonService(Proc);
            var fieldList = GetByField("ContactName", page, creator);
            return fieldList;
        }

        public List<string> GetContactArea(IPageable page, string creator)
        {
            return GetByField("Area", page, creator);
        }

        public List<string> GetContactAddress(IPageable page, string creator)
        {
            return GetByField("Address", page, creator);
        }

        public bool IsContactExist(string name, string cellphone, string creator)
        {
            var result = Proc.ExecuteScalar(ProcConst.Contact_GetIDByNameCellphone, name, cellphone, creator);
            return result == null ? false : true;
        }

        public List<ContactWithPhotoView> GetContactsWithPhoto(ContactFilter filter, IPageable page, ICountable records, string creator)
        {
            Pattern.SetModel(filter, GrainManage.Data.Pattern.PatternMode.Contain);
            DataSet contacts = Proc.ExecuteQuery(ProcConst.Contact_GetAllWithPhotoByFilterTimeExtent, filter.Name,
            filter.Area, filter.Address, filter.StartTime, filter.EndTime, GetOffset(page), page.PageSize, creator);
            return ConvertToModel<ContactWithPhotoView>(contacts, records);
        }

        public List<ContactTable> GetContacts(ContactFilter filter, IPageable page, ICountable records, string creator)
        {
            Pattern.SetModel(filter, GrainManage.Data.Pattern.PatternMode.Contain);
            DataSet contacts = Proc.ExecuteQuery(ProcConst.Contact_GetAllByFilterTimeExtent, filter.Name,
            filter.Area, filter.Address, filter.StartTime, filter.EndTime, GetOffset(page), page.PageSize, creator);
            return ConvertToModel<ContactTable>(contacts, records);
        }

        public int Insert(ContactTable contact)
        {
            Initializor.Initialize(contact, string.Empty);
            var result = Proc.ExecuteScalar(ProcConst.Contact_Insert, contact.ContactName, contact.Gender, contact.BirthDate, contact.Cellphone, contact.Telephone,
                        contact.QQ, contact.Email, contact.ImageID, contact.Area, contact.Address, contact.Remarks, contact.Creator);
            return result == null ? 0 : Convert.ToInt32(result);
        }

        public List<int> Insert(List<ContactTable> contacts)
        {
            var contactIDs = new List<int>();
            foreach (var contact in contacts)
            {
                contactIDs.Add(Insert(contact));
            }

            return contactIDs;
        }

        public int Update(ContactTable contact)
        {
            Initializor.Initialize(contact, string.Empty);
            return Proc.Execute(ProcConst.Contact_UpdateByID, contact.ContactID, contact.ContactName, contact.Gender, contact.BirthDate, contact.Cellphone,
                contact.Telephone, contact.QQ, contact.Email, contact.ImageID, contact.Area, contact.Address, contact.Remarks, contact.Creator);
        }

        public List<int> Update(List<ContactTable> contacts)
        {
            var invalidContactIDs = new List<int>();
            foreach (var contact in contacts)
            {
                if (Update(contact) == 0)
                {
                    invalidContactIDs.Add(contact.ContactID);
                }
            }
            return invalidContactIDs;
        }

        public int Delete(int id)
        {
            int count = Proc.Execute(ProcConst.Contact_DeleteByID, id);
            return count;
        }

        public List<int> Delete(List<int> idList)
        {
            var invalidList = new List<int>();
            foreach (var id in idList)
            {
                if (Delete(id) == 0)
                {
                    invalidList.Add(id);
                }
            }
            return invalidList;
        }

        #region 辅助函数

        private List<string> GetByField(string field, IPageable page, string creator)
        {
            var service = new CommonService(Proc);
            var fieldList = service.GetSpecifiedColumn("Contact", field, page, creator);
            return fieldList;
        }

        #endregion
    }
}
